﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
using System.Threading;
using System.IO;

namespace Game_Fly.GameSound
{
    public class GameSoundDX
    {
        private Stream musicPath;
        GameUI GameUI;
        public GameSoundDX(Stream musicPath)
        {
            this.GameUI = GameUI.GameForm;
            this.musicPath = musicPath;
        }

        public void Play()
        {
            Device secDev = new Device();//设备对象
            secDev.SetCooperativeLevel(GameUI, CooperativeLevel.Normal);//设置设备协作级别
            SecondaryBuffer secBuffer = new SecondaryBuffer(musicPath, secDev);//创建辅助缓冲区
            secBuffer.Play(0, BufferPlayFlags.Default);//设置缓冲区为默认播放
            Console.ReadLine();
        }

        delegate void DelegatePlay();
        public void ThreadPlay()
        {
            Thread t = new Thread(new ThreadStart(CorssThreadPlay));
            t.Start();
        }
        public void CorssThreadPlay()
        {
            if (GameUI.InvokeRequired)
            {
                DelegatePlay dp = new DelegatePlay(CorssThreadPlay);
                GameUI.Invoke(dp);
            }
            else
            {
                Device secDev;//设备对象    
                secDev = new Device();
                SecondaryBuffer secBuffer;//缓冲区对象               
                secDev.SetCooperativeLevel(GameUI, CooperativeLevel.Normal);//设置设备协作级别    
                secBuffer = new SecondaryBuffer(musicPath, secDev);//创建辅助缓冲区
                secBuffer.Play(0, BufferPlayFlags.Default);//设置缓冲区为默认播放
            }
        }
    }
}
